﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class BD: IBD
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public BD(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.BD GetBD(System.Int32 bDID)
		{
			BEGDC.PreContract.DAL.BD bD = BEGDC.PreContract.DAL.BD.TryFind(bDID);

            return bD;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetBD_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM BD";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteBD(System.Int32 bDID)
		{
			try
            {
				BEGDC.PreContract.DAL.BD bD = BEGDC.PreContract.DAL.BD.TryFind(bDID);
                bD.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("BD", string.Format("Error occured when deleting the BD, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddBD(System.String bDCode, Nullables.NullableInt32 sortNumber, Nullables.NullableDateTime createDate, System.Boolean active, System.String division, BEGDC.PreContract.DAL.BG bGID, Nullables.NullableDateTime updateDate, System.String bDDescription)	
		{
			try
            {
				BEGDC.PreContract.DAL.BD bD = new BEGDC.PreContract.DAL.BD();
				
				bD.BDCode = bDCode;
				bD.SortNumber = sortNumber;
				bD.CreateDate = createDate;
				bD.Active = active;
				bD.Division = division;
				bD.BGID = bGID;
				bD.UpdateDate = updateDate;
				bD.BDDescription = bDDescription;
				bD.CreateAndFlush();
				
				return bD.BDID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("BD", string.Format("Error occured when adding a new BD, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateBD(System.Int32 bDID, System.String bDCode, Nullables.NullableInt32 sortNumber, Nullables.NullableDateTime createDate, System.Boolean active, System.String division, BEGDC.PreContract.DAL.BG bGID, Nullables.NullableDateTime updateDate, System.String bDDescription)
		{
			try
            {
                BEGDC.PreContract.DAL.BD bD = GetBD(bDID);

				bD.BDCode = bDCode;
				bD.SortNumber = sortNumber;
				bD.CreateDate = createDate;
				bD.Active = active;
				bD.Division = division;
				bD.BGID = bGID;
				bD.UpdateDate = updateDate;
				bD.BDDescription = bDDescription;
				bD.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("BD", string.Format("Error occured when updating the BD, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



